package com.yangwei.examination.teacher.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yangwei.examination.teacher.model.BankQuestionParam;
import com.yangwei.examination.teacher.pojo.BankQuestion;
import com.yangwei.examination.teacher.pojo.BankQuestionAnswer;
import com.yangwei.examination.teacher.pojo.BankQuestionChoice;
import com.yangwei.examination.teacher.service.BankQuestionAnswerService;
import com.yangwei.examination.teacher.service.BankQuestionChoiceService;
import com.yangwei.examination.teacher.service.BankQuestionService;
import com.yangwei.examination.utils.ResultUtils;
import com.yangwei.examination.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Api(tags = "教师编辑题库",description = "题库相关")
@RestController
@RequestMapping("/tea/BankQuestion")
public class BankQuestionController {

    @Resource
    private BankQuestionService bankQuestionService;

    @Resource
    private BankQuestionAnswerService bankQuestionAnswerService;

    @Resource
    private BankQuestionChoiceService bankQuestionChoiceService;


    //保存试题到题库中
    @ApiOperation("保存单个试题")
    @PostMapping("/save")
    @Transactional
    public ResultVo save(@RequestBody BankQuestion bankQuestion) {

        boolean save = bankQuestionService.save(bankQuestion);

        //获取自增id
        Long bankQuestionId = bankQuestion.getId();

        if (save) {

            if (bankQuestion.getQuestionType().equals("1")||bankQuestion.getQuestionType().equals("2")) {

                //*保存选项内容列表
                for (BankQuestionChoice bankQuestionChoice : bankQuestion.getBankQuestionChoiceList()) {
                    bankQuestionChoice.setQuestionId(bankQuestionId);
                }
                bankQuestionChoiceService.saveBatch(bankQuestion.getBankQuestionChoiceList());

                //*保存单选答案号
                if (bankQuestion.getQuestionType().equals("1")) {
                    bankQuestion.getBankQuestionAnswerList().get(0).setQuestionId(bankQuestionId);
                    bankQuestionAnswerService.save(bankQuestion.getBankQuestionAnswerList().get(0));
                }

                //*保存多选答案号
                if (bankQuestion.getQuestionType().equals("2")) {
                   for (int select:bankQuestion.getSelected()){
                       bankQuestion.getBankQuestionAnswerList().get(select-1).setAnswerOrder((long) select);
                       bankQuestion.getBankQuestionAnswerList().get(select-1).setAnswerText(String.valueOf(select));
                       bankQuestion.getBankQuestionAnswerList().get(select-1).setQuestionId(bankQuestionId);
                   }

                    List<BankQuestionAnswer> bankQuestionAnswerList = bankQuestion.getBankQuestionAnswerList().stream()
                            .filter(questionAnswer -> questionAnswer.getAnswerText() != null && !questionAnswer.getAnswerText().isEmpty())
                            .collect(Collectors.toList());
//                   bankQuestion.setBankQuestionAnswerList(collect);
                    bankQuestionAnswerService.saveBatch(bankQuestionAnswerList);
                }

            }else if (bankQuestion.getQuestionType().equals("3")||bankQuestion.getQuestionType().equals("4")){
                List<BankQuestionAnswer> bankQuestionAnswerList =null;

                for (BankQuestionAnswer bankQuestionAnswer:bankQuestion.getBankQuestionAnswerList()){
                    bankQuestionAnswer.setQuestionId(bankQuestionId);
                }

                bankQuestionAnswerList = bankQuestion.getBankQuestionAnswerList().stream()
                        .filter(questionAnswer -> questionAnswer.getAnswerText() != null && !questionAnswer.getAnswerText().isEmpty())
                        .collect(Collectors.toList());

                bankQuestionAnswerService.saveBatch(bankQuestionAnswerList);
            }
        }

        return ResultUtils.success("试题添加成功！", bankQuestion);
    }

    //模糊查 条件查 查题库
    @ApiOperation("条件模糊查询题库")
    @PostMapping("/list")
    public ResultVo list(@RequestBody BankQuestionParam bankQuestionParam){

        List<BankQuestion> bankQuestionList = bankQuestionService.list(Wrappers.lambdaQuery(BankQuestion.class)
                .orderByDesc(BankQuestion::getId)
                .like(!StringUtils.isEmpty(bankQuestionParam.getSelectText()),BankQuestion::getQuestionTitle,bankQuestionParam.getSelectText())
                .eq(!StringUtils.isEmpty(bankQuestionParam.getQuestionType()),BankQuestion::getQuestionType,bankQuestionParam.getQuestionType())
                .eq(bankQuestionParam.getSubjectId()!=null,BankQuestion::getQuestionSubjectId,bankQuestionParam.getSubjectId())
        );

        for (BankQuestion bankQuestion:bankQuestionList){
            if (bankQuestion.getQuestionType().equals("1")||bankQuestion.getQuestionType().equals("2")){
                bankQuestion.setBankQuestionAnswerList(
                        bankQuestionAnswerService.list(Wrappers.lambdaQuery(BankQuestionAnswer.class)
                                .eq(BankQuestionAnswer::getQuestionId,bankQuestion.getId())
                        )
                );
                bankQuestion.setBankQuestionChoiceList(
                        bankQuestionChoiceService.list(Wrappers.lambdaQuery(BankQuestionChoice.class)
                                .eq(BankQuestionChoice::getQuestionId,bankQuestion.getId())
                        )
                );
            }else if (bankQuestion.getQuestionType().equals("3")||bankQuestion.getQuestionType().equals("4")){
                bankQuestion.setBankQuestionAnswerList(
                        bankQuestionAnswerService.list(Wrappers.lambdaQuery(BankQuestionAnswer.class)
                                .eq(BankQuestionAnswer::getQuestionId,bankQuestion.getId())
                        )
                );
            }

        }

        return ResultUtils.success("查询成功",bankQuestionList);
    }

    //用于统计
    @ApiOperation("统计题目个数")
    @GetMapping("/getBankQuestionCount")
    public ResultVo getBankQuestionCount(){
        int count = bankQuestionService.count();
        return ResultUtils.success("查询成功",count);
    }



}
